package com.example.yyy

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import androidx.core.app.NotificationCompat
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.toast

class SendLockNotify (context: Context, title: String, message: String, visibility:Boolean) {
    init {
        if (Build.VERSION.SDK_INT<Build.VERSION_CODES.LOLLIPOP){
            context.toast("锁屏需要安卓5.0以上")
        }else{
            context.toast("10秒后发送通知")

            val clickIntent = context.intentFor<MainActivity>()
            val piClick = PendingIntent.getActivity(
                context,
                R.string.app_name,
                clickIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )

            val builder: NotificationCompat.Builder = NotificationCompat.Builder(context, "5")
            builder.setContentIntent(piClick)
                .setAutoCancel(true)
                .setTicker("锁屏消息")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.tp))
                .setContentTitle(title)
                .setContentText(message)
                .setVisibility(if (visibility) Notification.VISIBILITY_PUBLIC as Int else Notification.VISIBILITY_PRIVATE as Int)

            val manager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                val channel = NotificationChannel("5", "channel5", NotificationManager.IMPORTANCE_DEFAULT)
                channel.enableLights(true)
                channel.lightColor = Color.RED
                channel.setShowBadge(true)
                manager.createNotificationChannel(channel)
                builder.setChannelId("5")
            }
            val n = builder.build()
            manager.notify(4, n)
        }
    }
}